package com.wisely.ch8_5.service.impl;

import com.wisely.ch8_5.dao.PersonRepository;
import com.wisely.ch8_5.domain.Person;
import com.wisely.ch8_5.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Created by thor on 17-3-23.
 */
@Service
public class DemoServiceImpl implements DemoService {
  @Autowired
  PersonRepository personRepository;

  @Override
  //@CachePut 新增或者更新到缓存
  @CachePut(value = "people", key = "#person.id")
  public Person save(Person person) {
    Person p = personRepository.save(person);
    System.out.println("为id,key为："+p.getId()+"数据做了缓存");
    return p;
  }

  @Override
  @CacheEvict(value = "people")
  public void remove(Long id) {
    System.out.println("删除了id/key为："+id+"的数据缓存");
    personRepository.delete(id);
  }

  @Override
  @Cacheable(value = "people", key = "#person.id")
  public Person findOne(Person person) {
    System.out.println("为id/key为："+person.getId()+"数据做了缓存");
    return personRepository.findOne(person.getId());
  }
}
